package com.ruoyi.hr.service.impl.welfare.welfarepayment;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.domain.welfare.welfarecalculationconfig.CcdBenefititem;
import com.ruoyi.hr.mapper.SkyMsgAlarmMapper;
import com.ruoyi.hr.mapper.welfare.welfareInfo.WelfareInfoMapper;
import com.ruoyi.hr.mapper.welfare.welfarecalculationconfig.CcdBenefititemMapper;
import com.ruoyi.hr.mapper.welfare.welfarepayment.WelfarePaymentRegisterMapper;
import com.ruoyi.hr.service.welfare.welfarepayment.WelfarePaymentRegisterService;
import com.ruoyi.hr.util.BaseResult;
import com.ruoyi.hr.util.ResultEnum;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WelfarePaymentRegisterServiceImpl implements WelfarePaymentRegisterService {

    @Autowired
    private CcdBenefititemMapper ccdBenefititemMapper;

    @Autowired
    WelfarePaymentRegisterMapper welfarePaymentRegisterMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    WelfareInfoMapper welfareInfoMapper;

    @Autowired
    SkyMsgAlarmMapper skyMsgAlarmMapper;

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findData(Map map2, List<Authority> authorities,int isAdmin) {
        String name = map2.get("name").toString();
        String badge = map2.get("badge").toString();
        List<CcdBenefititem> ccdBenefititems = ccdBenefititemMapper.findPayBack();
        //拼接Sql
        StringBuffer sql = new StringBuffer();
        for (CcdBenefititem ccdBenefititem : ccdBenefititems) {
            sql.append("(select Xvalue from CBENESUPPLY_ITEM_REGISTER  where SeqId = t.id and itemID = ");
            sql.append(ccdBenefititem.getId());
            sql.append(")" + ccdBenefititem.getTitle() + ",");
        }
        sql.delete(sql.length() - 1, sql.length());
        Map<String, Object> map = new HashMap<String,Object>();
        map.put("sql", sql);
        map.put("name",name);
        map.put("badge",badge);

        List<String> allIdList = new ArrayList<String>();
        if (!"".equals(map2.get("allId"))) {
            String[] array = map2.get("allId").toString().split(",");
            for (String id : array) {
                allIdList.add(id);
            }
        }
        map.put("allIdList", allIdList);

        List<Map<String, Object>> list = welfarePaymentRegisterMapper.findData(map, authorities,isAdmin);
        for (Map map1 : list) {
            for (CcdBenefititem ccdBenefititem : ccdBenefititems) {
                if (!map1.containsKey(ccdBenefititem.getTitle())) {
                    map1.put(ccdBenefititem.getTitle(), 0);
                }
            }
            if (!map1.containsKey("SupplyMonths")) {
                map1.put("SupplyMonths", ' ');
            }
            if (!map1.containsKey("remark")) {
                map1.put("remark", ' ');
            }
            if (!map1.containsKey("effectDate")) {
                map1.put("effectDate", " ");
            }
        }
        return list;
    }


    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int updateWelfarePaymentRegister(Map map) {
        String field = map.get("field").toString();
        String seqId = map.get("id").toString();
        if ("effectDate".equals(field) || "SupplyMonths".equals(field) || "remark".equals(field)) {
            //改主表
            return welfarePaymentRegisterMapper.updateRegister(map);
        } else {
            //改外键的值表
            //step1 为了查询是否有值先查询福利类型ID
            String itemId = ccdBenefititemMapper.findBenefititemIdbyTitle(field);
            //step2 根据类型和人员ID查询是否有值
            if (welfarePaymentRegisterMapper.findValueCount(itemId, seqId) == 0) {
                //insert
                map.put("itemId", itemId);
                return welfarePaymentRegisterMapper.insertValue(map);
            } else {
                //update
                return welfarePaymentRegisterMapper.updateValue(map);
            }
        }
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult addBeneSupplyAdd(String allPeid) {
        //叠加异常信息
        String msgAdd = "";
        try {
            //存储过程sql
            String sql;
            SysUser user = ShiroUtils.getSysUser();
            Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
            String[] array = allPeid.split(",");
            String msg;//异常信息
            for (String s : array) {
                //根据PEID取出人名
                Map<String, Object> map = welfareInfoMapper.findNameByPeid(s);
                sql = "declare @RetVal varchar(50) exec cSP_BeneSupplyAdd " + s + ",100," + maps.get("id").toString() + ",@RetVal output select @RetVal";
                String retVal = welfarePaymentRegisterMapper.executeProcedure(sql);
                msg = skyMsgAlarmMapper.getMsgById(retVal);
                if (msg != null) {
                    msgAdd += map.get("name") + ":" + msg + "\n";
                }
            }
        } catch (Exception e) {
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }
        if ("".equals(msgAdd)) {
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult procedure(String allId, String procedure) {
        //叠加异常信息
        String msgAdd = "";
        try {
            //存储过程sql
            String sql;
            SysUser user = ShiroUtils.getSysUser();
            Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
            String[] array = allId.split(",");
            String msg;//异常信息
            for (String s : array) {
                //根据id取出人名
                Map<String, Object> map = welfarePaymentRegisterMapper.findNameById(s);
                if ("cSP_BeneSupplyDec".equals(procedure)) {
                    sql = "declare @RetVal varchar(50) exec " + procedure + " " + s + ",@RetVal output select @RetVal";

                } else {
                    sql = "declare @RetVal varchar(50) exec " + procedure + " " + s + ",100," + maps.get("id").toString() + ",@RetVal output select @RetVal";
                }
                String retVal = welfarePaymentRegisterMapper.executeProcedure(sql);
                msg = skyMsgAlarmMapper.getMsgById(retVal);
                if (msg != null) {
                    msgAdd += map.get("name") + ":" + msg + "\n";
                }
            }
        } catch (Exception e) {
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }
        if ("".equals(msgAdd)) {
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }
}
